package io.mft.dev.bootstrap.tester;

import cn.hutool.json.JSONUtil;
import io.mft.commons.function.RequestValue;
import io.mft.dev.bootstrap.adapter.TestMFTRuningAdapter;
import io.mft.runtime.MFTApiExecutor;
import io.mft.runtime.function.ApiFunctionRequest;
import io.mft.runtime.function.ApiFunctionResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ApiFunctionTester {

	final ApiFunctionRequest request;
	final String funcNo;

	public ApiFunctionTester(String funcNo) throws Exception {
		this.request = new ApiFunctionRequest(new RequestValue());
		this.funcNo = funcNo;
	}

	public void addField(String key, String value) {
		request.getRequestValue().set(key, value);
	}

	public void testInvoke() {
		try {
			TestMFTRuningAdapter.start();
			log.info("请求入参:{}", request.getRequestValue());
			ApiFunctionResponse response = MFTApiExecutor.getInstance().execute(funcNo, request);
			log.info("请求响应:{}", response);
			response.getDsName().forEach(r -> {
				log.info("请求响应->>>结果集KEY:{}->>>数据:{}", r.getName(),JSONUtil.toJsonStr(response.listObject(r.getName())));
			});
		} catch (Throwable e) {
			log.error("", e);
		}

	}
}
